package com.lz.es;

import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.search.ClearScrollRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchScrollRequest;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @description
 *
 * @date 2021-11-26 10:40
 *
 * @author Lizhong
 */
public class EsPage {


    public SearchResponse createScroll(
            String deviceId, String type0, Integer rows, String scrollId) {
        SearchRequest searchRequest = new SearchRequest("iot");
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();

        if (StringUtils.isNotBlank(deviceId)) {
            queryBuilder = queryBuilder.must(QueryBuilders.termQuery ("deviceId", deviceId));
        }
        if (StringUtils.isNotBlank(type0)) {
            queryBuilder = queryBuilder.must(QueryBuilders.termQuery("type0", type0));
        }
        searchRequest.scroll(TimeValue.timeValueMinutes(1L));
        SearchResponse searchResponse = null;
        if (scrollId != null) {
            searchResponse = getClient().prepareSearchScroll(scrollId).get();
        } else {
            searchResponse = getClient().prepareSearch("iot").
                    addSort("type0", SortOrder.ASC)
                    .setScroll(TimeValue.timeValueMinutes(1L))
                    .setSize(rows)
                    .setQuery(queryBuilder).get();
        }
        // Arrays.stream(searchResponse.getHits().getHits()).forEach(System.out::println);
        return searchResponse;
    }

    private static TimeValue timeValue = new TimeValue(10000);

    public void getALL() {
        MatchAllQueryBuilder add = QueryBuilders.matchAllQuery();
        TransportClient client = getClient();
        SearchRequestBuilder setQuery = client.prepareSearch("iot")
                .setQuery(add)
                .setFrom(0)
                .setSize(10000)
                .setScroll(timeValue);
        SearchResponse actionGet = setQuery.execute().actionGet();
        List<Map<String, Object>> list = new ArrayList<>();
        while (true) {
            SearchHits hits = actionGet.getHits();
            SearchHit[] hits2 = hits.getHits();
            if (hits2.length < 1) {
                break;
            }
            for (SearchHit searchHit : hits2) {
                Map<String, Object> map = searchHit.getSourceAsMap();
                list.add(map);
            }
            String scrollID = actionGet.getScrollId();
            actionGet = client.prepareSearchScroll(scrollID)
                    .setScroll(timeValue).get();
        }
        System.out.println(list.size());

    }


    public static SearchResponse scroll(SearchScrollRequest scrollRequest) {
        try {
            return getClient().searchScroll(scrollRequest).get();
        } catch (Exception e) {
            return null;
        }
    }

    public static void clearScroll(ClearScrollRequest clearScrollRequest) {
        try {
            getClient().clearScroll(clearScrollRequest);
        } catch (Exception e) {
        }
    }

    public static TransportClient getClient() {
        EsTool instance = EsTool.getInstance();
        TransportClient client = instance.getClient();
        return client;
    }

}
